package com.example.zx_springboot.controller;

import com.example.zx_springboot.entity.ZxResource;
import com.example.zx_springboot.entity.Zxcustomer;
import com.example.zx_springboot.entity.Zxloss;
import com.example.zx_springboot.service.ZxMenuService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
public class ZxMenuControll {
    @Autowired
    private ZxMenuService zxMenuService;

    @PostMapping("/menu")
    public PageInfo getall() {


        List<ZxResource> list = zxMenuService.getall();

        List<ZxResource> parentNode = list.stream().filter(menu -> menu.getPid() == 0).collect(Collectors.toList());

        for (ZxResource menu : parentNode) {
            // 筛选所有数据中pid=父级id的数据就是二级菜单
            menu.setChildren(list.stream().filter(m -> menu.getResource_id()==(m.getPid())).collect(Collectors.toList()));}
            return new PageInfo<>(parentNode);

        }
        @PostMapping("/addmenu")
    public String addmenu(@RequestBody ZxResource zxResource){
        zxMenuService.addmenu(zxResource);
        return "add success";

        }
    @PostMapping("/updatemenu")
    public String updatemenu(@RequestBody ZxResource zxResource){
        zxMenuService.updatemenu(zxResource);
        return "update success";

    }
    @DeleteMapping("/deletemenu")
    public String deletemenu(@RequestParam("resource_id") Integer resource_id){
        zxMenuService.deletemenu(resource_id);

        return "delete success";
    }
    @PostMapping("/deletemenus")
    public String delettemenus(@RequestBody List<Integer>ids){
        zxMenuService.delettemenus(ids);
        return "delete success";
    }
}
